package linuxbox.common;

import javax.servlet.http.*;
import org.apache.log4j.Logger;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2002</p>
 * <p>Company: </p>
 * @author unascribed
 * @version 1.0
 */

public class BoxHomeSettingsAction extends Action
{
  protected static Logger logger = Logger.getLogger(BoxHomeSettingsAction.class.getName());
  protected static String editJSP = null;
  protected static String viewJSP = null;

  public void doAction(HttpSession aSession, BoxSession aBoxSession,
                       HttpServletRequest req, HttpServletResponse resp) throws Exception
  {
    String str = req.getParameter("SubmitCancel");
    if ( str != null )
    {
      sendFile(viewJSP,req,resp);
      return;
    }

    str = (String) req.getParameter("SubmitEdit");
    if ( str != null )
    {
      GetSettings(req, aBoxSession);
      sendFile(editJSP,req,resp);
      return;
    }
    str = (String) req.getParameter("SubmitSave");
    if ( str != null )
    {
      SaveSettings(req, aBoxSession);
      sendFile(viewJSP,req,resp);
      return;
    }
    sendFile(viewJSP,req,resp);
  }

  protected void SaveSettings(HttpServletRequest req, BoxSession session)
  {
    try
    {
      String str = (String) req.getParameter("TOP_PANE");
      session.setConfigString("home", "General", "Top Pane", str);

      str = (String) req.getParameter("BOTTOM_PANE");
      session.setConfigString("home", "General", "Bottom Pane", str);

      str = (String) req.getParameter("SHOW_SIDEMENU");

      session.setConfigBoolean("home", "General", "Show Side Menu", Boolean.valueOf(str).booleanValue());

      str = (String) req.getParameter("SHOW_PULLDOWNMENU");
      session.setConfigBoolean("home", "General", "Show Pulldown Menu", Boolean.valueOf(str).booleanValue());

      str = (String) req.getParameter("COLUMNS");
      session.setConfigInt("home", "General", "Columns", Integer.parseInt(str));

      String[] cols = (String[]) req.getParameterValues("COLUMN1");
      session.setConfigStrings("home", "General", "Column1", cols);

      cols = (String[]) req.getParameterValues("COLUMN2");
      session.setConfigStrings("home", "General", "Column2", cols);

      cols = (String[]) req.getParameterValues("COLUMN3");
      session.setConfigStrings("home", "General", "Column3", cols);
    }
    catch (Exception e)
    {
      //TODOXX: if input data error, should forward to the error page
        logger.error(e);
    }
  }

  protected void GetSettings(HttpServletRequest req, BoxSession session)
  {
    try
    {
      String str = session.getConfigString("home", "General", "Top Pane");
      req.setAttribute("TOP_PANE", str);

      str = session.getConfigString("home", "General", "Bottom Pane");
      req.setAttribute("BOTTOM_PANE", str);

      boolean b = session.getConfigBoolean("home", "General", "Show Side Menu");
      req.setAttribute("SHOW_SIDEMENU", String.valueOf(b));

      b = session.getConfigBoolean("home", "General", "Show Pulldown Menu");
      req.setAttribute("SHOW_PULLDOWNMENU", String.valueOf(b));

      int i = session.getConfigInt("home", "General", "Columns");
      req.setAttribute("COLUMNS", String.valueOf(i));

      String[] cols = session.getConfigStrings("home", "General", "Column1");
      if (cols != null)
        req.setAttribute("COLUMN1", cols);

      cols = session.getConfigStrings("home", "General", "Column2");
      if (cols != null)
        req.setAttribute("COLUMN2", cols);

      cols = session.getConfigStrings("home", "General", "Column3");
      if (cols != null)
        req.setAttribute("COLUMN3", cols);
    }
    catch (Exception e)
    {
      logger.error(e);
    }
  }

  public void setEditJSP(String str)
  {
    editJSP = str;
  }
  public void setViewJSP(String str)
  {
    viewJSP = str;
  }
}